//main panel(borderlayout)
//panelFor2x3Buttons(GridLayout, north) + helpButton(larger button, center) below panelFor2x3Buttons

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MenuOnBoard extends JPanel {

	private BuildDialog bd;
	private MortgageDialog md;
	private UnmortgageDialog ud;
	private TradeDialog td;
	
    private JButton btHelp, btTrade, btBuild, btMortgage, btUnmortgage, btEndTurn, btQuit;
    private JPanel panelFor3x2Buttons;
    private Player currentPlayer;
    
    private boolean buildClicked;
    private boolean mortgageClicked;
    private boolean unmortgageClicked;
    private boolean tradeClicked;
    private boolean endTurnClicked;
    private boolean quitClicked;
    
    public MenuOnBoard() {
        super();
        
        setLayout(new BorderLayout());
        
        panelFor3x2Buttons = new JPanel();
        panelFor3x2Buttons.setLayout(new GridLayout(3, 2));
        this.add(panelFor3x2Buttons, BorderLayout.CENTER);
        
        buildClicked = false;
        mortgageClicked = false;
        unmortgageClicked = false;
        tradeClicked = false;
        
        
        btHelp = new JButton(new ImageIcon("./menuButtons/helpbutton.png"));
        //btHelp.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));

        btTrade = new JButton("TRADE");
        btBuild = new JButton("BUILD");        
        btMortgage = new JButton("MORTGAGE/SELL");
        btUnmortgage = new JButton("UNMORTGAGE");
        btEndTurn = new JButton("END TURN");
        btQuit = new JButton("QUIT");

        ButtonListener listener = new ButtonListener();
        btHelp.addActionListener(listener);
        btTrade.addActionListener(listener);
        btBuild.addActionListener(listener);
        btMortgage.addActionListener(listener);
        btUnmortgage.addActionListener(listener);
        btEndTurn.addActionListener(listener);
        btQuit.addActionListener(listener);

        panelFor3x2Buttons.add(btTrade);
        panelFor3x2Buttons.add(btBuild);
        panelFor3x2Buttons.add(btMortgage);
        panelFor3x2Buttons.add(btUnmortgage);
        panelFor3x2Buttons.add(btEndTurn);
        panelFor3x2Buttons.add(btQuit);
        
        /*
        btTrade.setEnabled(false);
        btBuild.setEnabled(false);
        btMortgage.setEnabled(false);
        btUnmortgage.setEnabled(false);
        btEndTurn.setEnabled(false);
        */
        this.add(btHelp, BorderLayout.NORTH);
    }

    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
        	if(event.getSource() == btTrade){
        		tradeClicked = true;
        		new TradeWithWhoDialog();
        		tradeClicked = false;
        	}
            if(event.getSource() == btBuild){
                bd = new BuildDialog(currentPlayer);
                buildClicked = true;
            }
            if(event.getSource() == btQuit){
            	new QuitDialog();
            }
            if(event.getSource() == btMortgage){
            	mortgageClicked = true;
            	md = new MortgageDialog(currentPlayer);
            	mortgageClicked = false;
            }
            if(event.getSource() == btUnmortgage){
            	unmortgageClicked = true;
            	ud = new UnmortgageDialog(currentPlayer);
            	unmortgageClicked = false;
            }
            if(event.getSource() == btEndTurn){
            	endTurnClicked = true;
            	endTurnClicked = false;
            }
        }
    }
    
    public void setTradeButton(boolean state)
    {
    	btTrade.setEnabled(state);
    }
    
    public void setBuildButton(boolean state)
    {
    	btBuild.setEnabled(state);
    }

    public void setUnmortgageButton(boolean state)
    {
    	btUnmortgage.setEnabled(state);
    }
    
    public void setMortgageButton(boolean state)
    {
    	btMortgage.setEnabled(state);
    }

    public void setEndTurnButton(boolean state)
    {
    	btEndTurn.setEnabled(state);
    }
    
    public void setCurPlayer(Player p)
    {
    	currentPlayer = p;
    }
   
    public boolean buildButtonClicked()
    {
    	return buildClicked;
    }
    public boolean mortgageButtonClicked()
    {
    	return mortgageClicked;
    }
    public boolean unmortgageButtonClicked()
    {
    	return unmortgageClicked;
    }
    public boolean tradeButtonClicked()
    {
    	return tradeClicked;
    }
    public boolean endTurnClicked(){
    	return endTurnClicked;
    }
    public boolean quitClicked(){
    	return quitClicked;
    }
    
    public void resetBuildClicked(){
    	buildClicked = false;
    }
    public void resetMortgageClicked(){
    	mortgageClicked = false;
    }
    public void resetTradeClicked(){
    	tradeClicked = false;
    }
    public void resetUnmortgageClicked(){
    	unmortgageClicked = false;
    }
    
    //get return Value
    public int getBuildReturnValue(){
    	return bd.getReturnValue();
    }
    public int getMortgageReturnValue(){
    	return md.getReturnValue();
    }
    public int getUnmortgageReturnValue(){
    	return ud.getReturnValue();
    }
    /*public int getTradeReturnValue(){
    	return td.getReturnValue();
    }*/
    
    //Reset return value
    public void resetBuildReturnValue(){
    	bd.resetReturnValue();
    }
    public void resetMortgageReturnValue(){
    	md.resetReturnValue();
    }
    public void resetUnmortgageReturnValue(){
    	ud.resetReturnValue();
    }
    /*public void resetTradeReturnValue(){
    	td.resetReturnValue();
    }*/
    
  
    
}
class QuitDialog extends JDialog implements ActionListener{
	JButton okButton, cancelButton;
	QuitDialog(){
		JPanel panel = new JPanel(new GridLayout(2,1));//contains panel2 and panel3
		JPanel panel2 = new JPanel();// contains a label
		JPanel panel3 = new JPanel(new FlowLayout());// contains two buttons, ok and cancel
		panel.add(panel2);
		panel.add(panel3);
		okButton = new JButton("OK");
		cancelButton = new JButton("Cancel");
		JLabel question = new JLabel("     Would you like to quit the game?      ");
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		panel2.add(question);
		panel3.add(okButton);
		panel3.add(cancelButton);
		
		this.add(panel);
		this.setSize(300, 120);
		this.setLocationRelativeTo(null);
		this.setModal(true);
		this.setVisible(true);
		
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == okButton){
			System.exit(0);
		}
		else{
			this.setVisible(false);
		}
	}
}
